package cn.donting.jme.edit.attribute;

import com.jme3.export.*;
import javafx.scene.Node;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;


public abstract class Attribute<T>  implements Savable{
    private T value;
    @Getter
    @Setter
    private String title="";

    private final List<ValueChangeListener> attributeListenerList = new ArrayList<>();

    public Attribute(String title) {
        this.title = title;
    }

    public Attribute() {
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        T oldValue = this.value;
        this.value = value;
        for (ValueChangeListener attributeListener : attributeListenerList) {
            attributeListener.valueChange(oldValue, value);
        }
    }


    public abstract Node getUINode();

    public void addListener(ValueChangeListener<T> attributeListener) {
        attributeListenerList.add(attributeListener);
    }

    public boolean removeListener(ValueChangeListener<T> attributeListener) {
        return attributeListenerList.remove(attributeListener);
    }

    @Override
    public void write(JmeExporter ex) throws IOException {
        OutputCapsule capsule = ex.getCapsule(this);
        capsule.write(title,"title","");
    }

    @Override
    public void read(JmeImporter im) throws IOException {
        InputCapsule capsule = im.getCapsule(this);
        capsule.readString("title","unknown");
    }
}
